<?php
class StudentSearch extends Controller {
	function StudentSearch()
	{
		parent::Controller();
	}

	function index()
	{
	 	redirect('login/index');
	}
	
	function application()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		
	    $this->load->view('header');
		$this->load->view('student-search-form', $data);
		$this->load->view('footer');
	}
	
	function delete(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('StudentModel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$this->StudentModel->delete($id[$i]);
	    }	    
	    $this->applicationsearch();
	}
	
	function applicationsearch()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $criteria = array();
	    
	    $criteria['name'] = $this->input->post('txtName') ? $this->input->post('txtName') : null;
	    $criteria['subject'] = $this->input->post('selSubjet') ? $this->input->post('selSubjet') : null;
	    
	    $criteria['txtASchool'] = $this->input->post('txtASchool') ? $this->input->post('txtASchool') : null;
	    $criteria['txtASaveDate'] = $this->input->post('txtASaveDate') ? $this->input->post('txtASaveDate') : null;
	    $criteria['txtAMonthBirth'] = $this->input->post('txtAMonthBirth') ? $this->input->post('txtAMonthBirth') : null;
	    $criteria['txtAYearBirth'] = $this->input->post('txtAYearBirth') ? $this->input->post('txtAYearBirth') : null;
	    $sc = $this->input->post('chkSchool');
		$schoolwhere = "";
		if($sc) {
			$schoolwhere = " and stusc.shool_id in ( ";
			for($ii = 0; $ii < count($sc); $ii++) {
				$schoolwhere .= $sc[$ii];
				if($ii + 1 < count($sc)) {
					$schoolwhere .= ",";
				}
			}
			$schoolwhere .= ") ";
		}
		$criteria['school'] = $schoolwhere;
		
	    $levelFrom = $this->input->post('txtClassFrom') ? $this->input->post('txtClassFrom') : null;
	    $levelTo = $this->input->post('txtClassTo') ? $this->input->post('txtClassTo') : null;
	    $classWhereSql = '';
	    if($this->input->post('txtClassFrom') != null || $this->input->post('txtClassTo') != null) {
	    	$allClass = array('ต่ำกว่า อ. 1', 'อ. 1', 'อ. 2', 'อ. 3', 'ป. 1', 'ป. 2', 'ป. 3', 'ป. 4', 'ป. 5', 'ป. 6',
	    				 'ม. 1', 'ม. 2', 'ม. 3', 'ม. 4', 'ม. 5', 'ม. 6', 'ปี 1', 'ปี 2', 'ปี 3', 'ปี 4', 'สูงกว่า ป. ตรี ');
	    	
	    	if($this->input->post('txtClassFrom') != null) {
	    		$isConditionFrom = false;
	    		$isConditionTo = false;
	    		foreach ($allClass as $c) {
	    			if($c == $this->input->post('txtClassFrom')) {
	    				$isConditionFrom = true;
	    			}
	    			
	    			if($isConditionFrom == false) {
	    				$classWhereSql .= " and current_class !=  '" . $c . "' ";
	    			}
	    			
	    			if($isConditionFrom) {
	    				if($isConditionTo == true) {
		    				$classWhereSql .= " and current_class !=  '" . $c . "' ";
		    			}
	    				if($c == $this->input->post('txtClassTo')) {
	    					$isConditionTo = true;
	    				}
	    			}
	    		}
	    	} else {
	    		$isConditionTo = false;
	    		foreach ($allClass as $c) {
	    			if($isConditionTo == true) {
	    				$classWhereSql .= " and current_class !=  '" . $c . "' ";
	    			}
    				if($c == $this->input->post('txtClassTo')) {
    					$isConditionTo = true;
    				}
	    		}
	    	}
	    } 
	    $criteria['class_level'] = $classWhereSql;
	    $criteria['location'] = $this->input->post('txtLocation') ? $this->input->post('txtLocation') : null;
	    $criteria['studied_day'] = $this->input->post('selStartDay') ? $this->input->post('selStartDay') : null;
	    $criteria['tel'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $criteria['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;
	    $criteria['nick_name'] = $this->input->post('txtNickName') ? $this->input->post('txtNickName') : null;
	    $this->load->model('StudentModel');
	    if($this->session->userdata('profile') == 'employee' && $criteria['name'] == null &&
	    	$criteria['nick_name'] == null && $criteria['tel'] == null && $criteria['email'] == null) {
	    		$result['result'] = null; 
	    	} else {
	    		$result['result'] = $this->StudentModel->tutorlandSearch($criteria);
	    	}
	    $this->load->model('SchoolModel');
		$result['schoolList'] = $this->SchoolModel->getAll();
	    $this->load->view('header');
		$this->load->view('student-search-form', $result);
		$this->load->view('footer');
	}
	
	function centerdb()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('StudentModel');
	    $stuID = $this->input->post('txtDel');
	    
	    for($i = 0; $i < count($stuID); $i++) {
    		$this->StudentModel->delete($stuID[$i]);
	    }
	    $this->msg = "บันทึกข้อมูลเรียบร้อย";
	    $this->index();
	}
}

?>